package com.vgemv.jsutilitysdk.JsNetworkManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.telephony.TelephonyManager;
import android.util.Log;

public class JsNetworkStateBroadcastReceiver extends BroadcastReceiver {

    //全局变量统一用方法去获取
    private static JsNetworkStateBroadcastReceiver _sharedInstance = new JsNetworkStateBroadcastReceiver();

    public static JsNetworkStateBroadcastReceiver sharedInstance() {
        return _sharedInstance;
    }


    /**
     * 初始监听会发送初始状态.
     * @param context
     * @param intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("network","Receive network change");
        //WIFI状态改变.
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            //拿到wifi的状态值
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_NEW_STATE, 0);
            Log.i("Net", "wifiState = " + wifiState);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
            }
        }
        //监听wifi的连接状态即是否连接的一个有效的无线路由
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (parcelableExtra != null) {
                // 获取联网状态的NetWorkInfo对象
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                //获取的State对象则代表着连接成功与否等状态
                NetworkInfo.State state = networkInfo.getState();
                //判断网络是否已经连接
                boolean isConnected = state == NetworkInfo.State.CONNECTED;
                Log.i("Net", "isConnected:" + isConnected);
                if (isConnected) {

                } else {

                }
            }
        }

        // 监听网络连接，包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            //获取联网状态的NetworkInfo对象
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                String connectionType = getConnectionType(info);
                switch (info.getState()) {
                    case CONNECTED: {
                        Log.i("Net",connectionType + "连上");
                        break;
                    }
                    case DISCONNECTED: {
                        Log.i("Net",connectionType + "断开");
                        break;
                    }
                    default:{
                        break;
                    }
                }
            }
        }
    }

    private String getConnectionType(NetworkInfo info) {
        int connectType = info.getType();
        switch (connectType) {
            case ConnectivityManager.TYPE_MOBILE: {
                int connectSubType = info.getSubtype();
                switch (connectSubType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        return "移动数据2G";
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                        return "移动数据3G";
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        return "移动数据4G";

                    default:{
                        return "移动数据";
                    }
                }
            }
            case ConnectivityManager.TYPE_WIFI: {
                return "Wifi";
            }
            default: {
                return "";
            }
        }
    }
}
